package com.miya.demo.support.netty.enums;

import com.miya.demo.common.exception.BusinessException;
import lombok.AllArgsConstructor;
import lombok.Getter;

import java.util.EnumSet;
import java.util.Set;

/**
 * 消息类型枚举
 *
 * @author CaiXiaowei
 * @date 2023/1/17
 */
@Getter
@AllArgsConstructor
public enum MessageActionEnum {

    CONNECT(1,"第一次（或重连）初始化连接"),
    CHAT(2,"聊天消息"),
    SIGNED(3,"消息签收"),
    KEEPALIVE(4,"客户端保持心跳"),
    PULL_FRIEND(5, "拉取好友");

    public final Integer code;
    public final String content;

    private static final Set<MessageActionEnum> ALL = EnumSet.allOf(MessageActionEnum.class);

    public static MessageActionEnum getByCode(Integer code) {
        if (code == null) {
            return null;
        }

        return ALL.stream()
                .filter(o -> o.code.equals(code))
                .findAny().orElseThrow(() -> new BusinessException("消息类型: " + code + "未匹配到相关值！"));
    }

    public boolean is(Integer code) {
        return getCode().equals(code);
    }

}
